package cn.myframe.task;

import cn.myframe.dto.StockBaseDto;
import cn.myframe.redis.RedisService;
import com.alibaba.fastjson.JSON;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;

/**
 * 定时更新股票列表
 * @Author: ynz
 * @Date: 2019/3/19/019 16:51
 * @Version 1.0
 */
@Component
public class UpdateStockList {

    private final static String STOCK_KEY = "STOCK_LIST";

    private final static String STOCK_BASE = "STOCK:BASE";

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    RedisService redisService;

    public void down(){
        // 小文件
        RequestEntity requestEntity = null;
        try {
            requestEntity = RequestEntity.get(
                    new URI("http://stock.gtimg.cn/data/get_hs_xls.php?id=ranka&type=1&metric=chr")).build();
            ResponseEntity<byte[]> responseEntity = restTemplate.exchange(requestEntity, byte[].class);
            byte[] downloadContent = responseEntity.getBody();
            InputStream inputStream = new ByteArrayInputStream(downloadContent);
            Workbook work =  new HSSFWorkbook(inputStream);
            Sheet sheet = null;
            Row row = null;
            Cell cell = null;
            for (int i = 0; i < work.getNumberOfSheets(); i++) {
                sheet = work.getSheetAt(i);
                if (sheet == null) {
                    continue;
                }
                for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                    row = sheet.getRow(j);
                    if (row == null || row.getFirstCellNum() == j || j<2) {
                        continue;
                    }

                    StockBaseDto dto = StockBaseDto.builder()
                            .stockCode(row.getCell(0).getStringCellValue())
                            .stockName(row.getCell(1).getStringCellValue())
                            .stockPrice(row.getCell(2).getNumericCellValue())
                            .increase(row.getCell(3).getNumericCellValue())
                            .volume((int)row.getCell(7).getNumericCellValue())
                            .build();
                    RealTimeTrading.stockBaseDtoList.add(dto);
                    System.out.println(JSON.toJSONString(dto));

                    redisService.addList(STOCK_KEY,row.getCell(0).getStringCellValue());
                    redisService.hmSet(STOCK_BASE,dto.getStockCode(),JSON.toJSONString(dto));

                }
            }
            work.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
